from invfun import inv, inv_1
from gear import HiddenPrints, PrintToTxt, GearInit, PlanetInit, Gear, PlanetaryGear
from math import sin, cos, tan, radians, degrees, floor, sqrt, acos, atan, pi
import numpy as np
from shichuan_stiff import pt_stiffness

def calcx1(x2, x3, z1, z3, z2, alpha=radians(20)):  # 根据x2,x3，计算两个啮合角，最后计算出x1
    z_segma_13 = z1+z3
    z_segma_32 = z2-z3
    x_segma_32 = x2-x3
    alpha_work_32 = inv_1(
        x_segma_32*2*tan(alpha)/z_segma_32+inv(alpha))
    alpha_work_13 = acos(
        z_segma_13/z_segma_32*cos(alpha_work_32))
    x_segma_13 = z_segma_13 * \
        (inv(alpha_work_13)-inv(alpha))/(2*tan(alpha))
    x1 = x_segma_13 - x3
    return x1


def get_sorted_ptgear(m, z1, z3, z2, x2range, x3range, alpha=radians(20)):  # 得到按某种顺序排列的行星齿轮对象列表
    pt_objects = []  # 行星齿轮对象列表，用于储存按某种顺序排列的行星齿轮对象，并最后返回
    for x2 in x2range:
        for x3 in x3range:
            x1 = calcx1(x2, x3, z1, z3, z2, alpha)
            ptInit = PlanetInit()
            pt = PlanetaryGear(m, z1, z3, z2, x1, x3, x2, ptInit)
            if (pt.ret_ac and pt.ret_cb): # 满足干涉、齿顶厚等限制条件
                #dy1 = pt.gear_ac.dyeta
                #dy2 = pt.gear_cb.dyeta
                ep1 = pt.gear_ac.ContactRatio
                ep2 = pt.gear_cb.ContactRatio
                if ep1 >= 1.3 and ep2 >= 1.3: # 满足基本的重合度条件
                    #_,maxmin = pt_stiffness(pt,xstep=radians(0.01),savefilename='None',show=False)
                    #pt.gear_ac.st_jump_max = maxmin[0][2]
                    #pt.gear_cb.st_jump_max = maxmin[1][2]
                    pt_objects.append(pt)

                '''
                if pt.gear_ac.dyeta < 0.1:
                    dy1 = pt.gear_ac.dyeta
                    dy2 = pt.gear_cb.dyeta
                    ep1 = pt.gear_ac.ContactRatio
                    ep2 = pt.gear_cb.ContactRatio
                    print('x1:%.4f,x3:%.3f,x2:%.3f,\tD_Yeta:%.4f,%.4f,\tCRatio:%.4f,%.4f\tA_w:%.3f,%.3f' % (
                        x1, x3, x2, dy1, dy2, ep1, ep2, degrees(pt.gear_ac.alpha_work), degrees(pt.gear_cb.alpha_work)))
                '''
            #pt_objects.sort(key=lambda pt:pt.gear_ac.dyeta)
            pt_objects.sort(key=aimFun) # 以目标函数进行排序，从小到大
    return pt_objects


def aimFun(pt: PlanetaryGear): # 用于排序行星齿轮对象的目标函数
    aim = 10*pt.gear_ac.dyeta + pt.gear_cb.dyeta
    return aim


if __name__ == '__main__':
    hh = 0.01
    m = 2
    z1 = 27
    z2 = 108
    z3 = 41
    z_segma_13 = z1+z3
    z_segma_32 = z2-z3
    alpha = radians(20)

    x2range = np.arange(0.3, 0.6, hh)
    x3range = np.arange(-0.2, 0, hh)

    pt_res = get_sorted_ptgear(m, z1, z3, z2, x2range, x3range, alpha)
    print(len(pt_res))
    for i in range(10):

        
        #pt_stiffness(pt_res[i],xstep=radians(0.1),savefilename='None',show=False)
        pt_res[i].MiniPrint()
        #print(pt_res[i].gear_ac.st_jump_max,pt_res[i].gear_cb.st_jump_max)
    '''
        print(pt_res[i].gear_ac.ContactRatio1-pt_res[i].gear_ac.ContactRatio2)
        print(pt_res[i].gear_cb.ContactRatio1-pt_res[i].gear_cb.ContactRatio2)
    '''
    '''
    z1 = 18
    z2 = 108
    z3 = 45
    ptInit = PlanetInit()
    
    x1 = calcx1(0.45,0.0285,z1,z3,z2,alpha)
    pt = PlanetaryGear(m, z1, z3, z2, x1, 0.0285, 0.45, ptInit)
    pt.PrintItem()
    '''
